package com.jin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jin.bean.Material;
import com.jin.common.ResponseResult;
import com.jin.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created on 2025/5/14.
 * @author Zhongyuan Jin
 */
@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    //获取全部材料信息
    @GetMapping("/all")
    public ResponseResult<List<Material>> getAllMaterials() {
        List<Material> materials = materialService.list();
        return ResponseResult.success("获取全部材料信息", materials);
    }

    //查询所有材料信息（支持分页）
    @GetMapping("/list")
    public ResponseResult<List<Material>> list(@RequestParam(defaultValue = "1") int pageNum,
                                               @RequestParam(defaultValue = "10") int pageSize) {
        Page<Material> page = new Page<>(pageNum, pageSize);
        Page<Material> materialPage = materialService.page(page);
        List<Material> records = materialPage.getRecords();
        long total = materialPage.getTotal();
        return ResponseResult.success("查询所有材料信息（支持分页）",records,total);
    }

    //根据 ID 查询材料信息
    @GetMapping("/{id}")
    public ResponseResult<Material> getById(@PathVariable Integer id) {
        Material material = materialService.getById(id);
        if (material == null) {
            return ResponseResult.fail("未找到该材料", null);
        }
        return ResponseResult.success("找到该材料", material);
    }

    //新增材料信息
    @PostMapping
    public ResponseResult<Boolean> add(@RequestBody Material material) {
        boolean result = materialService.save(material);
        return new ResponseResult<>(result ? 0 : 1, result ? "添加成功" : "添加失败", result,  null);
    }

    //更新材料信息
    @PutMapping
    public ResponseResult<Boolean> update(@RequestBody Material material) {
        boolean result = materialService.updateById(material);
        return new ResponseResult<>(result ? 0 : 1, result ? "更新成功" : "更新失败", result,  null);
    }

    //删除材料信息
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable Integer id) {
        boolean result = materialService.removeById(id);
        return new ResponseResult<>(result ? 0 : 1, result ? "删除成功" : "删除失败", result,  null);
    }

    //启用材料
    @PutMapping("/enable/{id}")
    public ResponseResult<Boolean> enable(@PathVariable Integer id) {
        Material material = materialService.getById(id);
        if (material == null) {
            return ResponseResult.fail("未找到该材料", false);
        }
        material.setIsEnable(1); // 启用
        boolean result = materialService.updateById(material);
        return new ResponseResult<>(result ? 0 : 1, result ? "启用成功" : "启用失败", result,  null);
    }

    //禁用材料
    @PutMapping("/disable/{id}")
    public ResponseResult<Boolean> disable(@PathVariable Integer id) {
        Material material = materialService.getById(id);
        if (material == null) {
            return ResponseResult.fail("未找到该材料", false);
        }
        material.setIsEnable(0); // 禁用
        boolean result = materialService.updateById(material);
        return new ResponseResult<>(result ? 0 : 1, result ? "禁用成功" : "禁用失败", result,  null);
    }

    //按名称模糊查询
    @GetMapping("/search")
    public ResponseResult<List<Material>> searchByName(@RequestParam String materialName) {
        List<Material> materials = materialService.list(new QueryWrapper<Material>().like("material_name", materialName));
        return ResponseResult.success("按名称模糊查询", materials);
    }

}